<!DOCTYPE html>
<html lang="en">
  <head>
    <title>
        Java-Senior-JVM - rulerLwx Blog
      </title>
        <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
      content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no, minimal-ui">
    <meta name="renderer" content="webkit">
    <meta http-equiv="Cache-Control" content="no-transform" />
    <meta http-equiv="Cache-Control" content="no-siteapp" />
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="format-detection" content="telephone=no,email=no,adress=no">
    
    <meta name="theme-color" content="#000000" />
    
    <meta http-equiv="window-target" content="_top" />
    
    
    <meta name="description" content="JVM体系结构 JVM的位置 JVM运行在操作系统之上，与硬件没有直接交互 JVM体系结构 灰色代表线程独有，亮色代表线程共享 绿色的就相当于Java" />
    <meta name="generator" content="Hugo 0.73.0 with theme pure" />
    <title>Java-Senior-JVM - rulerLwx Blog</title>
    
    
    <link rel="stylesheet" href="https://rulerLwx.gitee.io/css/style.min.c4bc7071f132c964c2116bca53b392933f377e5ca7b7051ed245187c621a2d3e.css">
    
    <link rel="stylesheet" href="https://cdn.staticfile.org/highlight.js/9.15.10/styles/github.min.css" async>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/tocbot/4.4.2/tocbot.css" async>
    <meta property="og:title" content="Java-Senior-JVM" />
<meta property="og:description" content="JVM体系结构 JVM的位置 JVM运行在操作系统之上，与硬件没有直接交互 JVM体系结构 灰色代表线程独有，亮色代表线程共享 绿色的就相当于Java" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://rulerLwx.gitee.io/2020/07/java-senior-jvm/" />
<meta property="article:published_time" content="2020-07-01T18:58:47+08:00" />
<meta property="article:modified_time" content="2020-07-01T18:58:47+08:00" />
<meta itemprop="name" content="Java-Senior-JVM">
<meta itemprop="description" content="JVM体系结构 JVM的位置 JVM运行在操作系统之上，与硬件没有直接交互 JVM体系结构 灰色代表线程独有，亮色代表线程共享 绿色的就相当于Java">
<meta itemprop="datePublished" content="2020-07-01T18:58:47&#43;08:00" />
<meta itemprop="dateModified" content="2020-07-01T18:58:47&#43;08:00" />
<meta itemprop="wordCount" content="3580">



<meta itemprop="keywords" content="jvm," /><meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="Java-Senior-JVM"/>
<meta name="twitter:description" content="JVM体系结构 JVM的位置 JVM运行在操作系统之上，与硬件没有直接交互 JVM体系结构 灰色代表线程独有，亮色代表线程共享 绿色的就相当于Java"/>

    <!--[if lte IE 9]>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/classlist/1.1.20170427/classList.min.js"></script>
      <![endif]-->

    <!--[if lt IE 9]>
        <script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
      <![endif]-->
  </head>

  
  

  <body class="main-center theme-black" itemscope itemtype="http://schema.org/WebPage"><header class="header" itemscope itemtype="http://schema.org/WPHeader">
    <div class="slimContent">
      <div class="navbar-header">
        <div class="profile-block text-center">
          <a id="avatar" href="https://gitee.com/rulerLwx" target="_blank">
            <img class="img-circle img-rotate" src="https://rulerLwx.gitee.io/avatar.png" width="200" height="200">
          </a>
          <h2 id="name" class="hidden-xs hidden-sm">rulerLwx</h2>
          <h3 id="title" class="hidden-xs hidden-sm hidden-md">thinking...</h3>
          <small id="location" class="text-muted hidden-xs hidden-sm"><i class="icon icon-map-marker"></i>Guangzhou, China</small>
        </div><div class="search" id="search-form-wrap">
    <form class="search-form sidebar-form">
        <div class="input-group">
            <input type="text" class="search-form-input form-control" placeholder="Search" />
            <span class="input-group-btn">
                <button type="submit" class="search-form-submit btn btn-flat" onclick="return false;"><i
                        class="icon icon-search"></i></button>
            </span>
        </div>
        <div class="ins-search">
            <div class="ins-search-mask"></div>
            <div class="ins-search-container">
                <div class="ins-input-wrapper">
                    <input type="text" class="ins-search-input" placeholder="Type something..."
                        x-webkit-speech />
                    <button type="button" class="close ins-close ins-selectable" data-dismiss="modal"
                        aria-label="Close"><span aria-hidden="true">×</span></button>
                </div>
                <div class="ins-section-wrapper">
                    <div class="ins-section-container"></div>
                </div>
            </div>
        </div>
    </form>
</div>
        <button class="navbar-toggle collapsed" type="button" data-toggle="collapse" data-target="#main-navbar" aria-controls="main-navbar" aria-expanded="false">
          <span class="sr-only">Toggle navigation</span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
        </button>
      </div>
      <nav id="main-navbar" class="collapse navbar-collapse" itemscope itemtype="http://schema.org/SiteNavigationElement" role="navigation">
        <ul class="nav navbar-nav main-nav">
            <li class="menu-item menu-item-home">
                <a href="/">
                    <i class="icon icon-home-fill"></i>
                  <span class="menu-title">Home</span>
                </a>
            </li>
            <li class="menu-item menu-item-archives">
                <a href="/posts/">
                    <i class="icon icon-archives-fill"></i>
                  <span class="menu-title">Archives</span>
                </a>
            </li>
            <li class="menu-item menu-item-categories">
                <a href="/categories/">
                    <i class="icon icon-folder"></i>
                  <span class="menu-title">Categories</span>
                </a>
            </li>
            <li class="menu-item menu-item-tags">
                <a href="/tags/">
                    <i class="icon icon-tags"></i>
                  <span class="menu-title">Tags</span>
                </a>
            </li>
            <li class="menu-item menu-item-about">
                <a href="/about/">
                    <i class="icon icon-cup-fill"></i>
                  <span class="menu-title">About</span>
                </a>
            </li>
        </ul>
      </nav>
    </div>
  </header>

<aside class="sidebar" itemscope itemtype="http://schema.org/WPSideBar">
  <div class="slimContent">
    
      <div class="widget">
    <h3 class="widget-title">Board</h3>
    <div class="widget-body">
        <div id="board">
            <div class="content">enjoy~
            </div>
        </div>
    </div>
</div>

      <div class="widget">
    <h3 class="widget-title"> Categories</h3>
    <div class="widget-body">
        <ul class="category-list">
            <li class="category-list-item"><a href="https://rulerLwx.gitee.io/categories/java-framework/" class="category-list-link">java-framework</a><span class="category-list-count">38</span></li>
            <li class="category-list-item"><a href="https://rulerLwx.gitee.io/categories/java-front-end/" class="category-list-link">java-front-end</a><span class="category-list-count">11</span></li>
            <li class="category-list-item"><a href="https://rulerLwx.gitee.io/categories/java-se/" class="category-list-link">java-se</a><span class="category-list-count">21</span></li>
            <li class="category-list-item"><a href="https://rulerLwx.gitee.io/categories/java-senior/" class="category-list-link">java-senior</a><span class="category-list-count">4</span></li>
            <li class="category-list-item"><a href="https://rulerLwx.gitee.io/categories/linux/" class="category-list-link">linux</a><span class="category-list-count">13</span></li>
            <li class="category-list-item"><a href="https://rulerLwx.gitee.io/categories/tools/" class="category-list-link">tools</a><span class="category-list-count">1</span></li>
            <li class="category-list-item"><a href="https://rulerLwx.gitee.io/categories/%E6%8A%80%E6%9C%AF%E6%9D%82%E7%83%A9/" class="category-list-link">技术杂烩</a><span class="category-list-count">4</span></li>
            <li class="category-list-item"><a href="https://rulerLwx.gitee.io/categories/%E6%95%B0%E6%8D%AE%E5%BA%93/" class="category-list-link">数据库</a><span class="category-list-count">15</span></li>
        </ul>
    </div>
</div>
      <div class="widget">
    <h3 class="widget-title"> Tags</h3>
    <div class="widget-body">
        <ul class="tag-list">
            
            
            <li class="tag-list-item"><a href="https://rulerLwx.gitee.io/tags/jvm/" class="tag-list-link">jvm</a><span
                    class="tag-list-count">1</span></li>
            
            
            <li class="tag-list-item"><a href="https://rulerLwx.gitee.io/tags/%E5%A4%9A%E7%BA%BF%E7%A8%8B/" class="tag-list-link">多线程</a><span
                    class="tag-list-count">2</span></li>
            
            
            <li class="tag-list-item"><a href="https://rulerLwx.gitee.io/tags/%E7%BD%91%E7%BB%9C%E7%BC%96%E7%A8%8B/" class="tag-list-link">网络编程</a><span
                    class="tag-list-count">3</span></li>
            
        </ul>

    </div>
</div>
      
<div class="widget">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget-body">
        <ul class="recent-post-list list-unstyled no-thumbnail">
            <li>
                <div class="item-inner">
                    <p class="item-title">
                        <a href="https://rulerLwx.gitee.io/2020/07/%E8%87%AA%E5%B7%B1%E5%8A%A8%E6%89%8B%E4%B8%80%E6%89%B9%E9%87%8F%E7%A7%BB%E5%8A%A8%E9%87%8D%E5%91%BD%E5%90%8D%E6%96%87%E4%BB%B6/" class="title">自己动手（一）——批量移动、重命名文件</a>
                    </p>
                    <p class="item-date">
                        <time datetime="2020-07-27 17:50:02 &#43;0800 CST" itemprop="datePublished">2020-07-27</time>
                    </p>
                </div>
            </li>
            <li>
                <div class="item-inner">
                    <p class="item-title">
                        <a href="https://rulerLwx.gitee.io/2020/07/%E5%85%B3%E4%BA%8Einteger%E7%9A%84-128~127%E7%BC%93%E5%AD%98/" class="title">关于Integer的 -128~127缓存</a>
                    </p>
                    <p class="item-date">
                        <time datetime="2020-07-11 16:56:21 &#43;0800 CST" itemprop="datePublished">2020-07-11</time>
                    </p>
                </div>
            </li>
            <li>
                <div class="item-inner">
                    <p class="item-title">
                        <a href="https://rulerLwx.gitee.io/2020/07/%E8%B7%A8%E5%9F%9F%E9%97%AE%E9%A2%98/" class="title">跨域问题</a>
                    </p>
                    <p class="item-date">
                        <time datetime="2020-07-08 22:41:12 &#43;0800 CST" itemprop="datePublished">2020-07-08</time>
                    </p>
                </div>
            </li>
            <li>
                <div class="item-inner">
                    <p class="item-title">
                        <a href="https://rulerLwx.gitee.io/2020/07/%E4%B8%AA%E4%BA%BA%E5%8D%9A%E5%AE%A2%E6%90%AD%E5%BB%BA/" class="title">个人博客搭建</a>
                    </p>
                    <p class="item-date">
                        <time datetime="2020-07-05 18:58:47 &#43;0800 CST" itemprop="datePublished">2020-07-05</time>
                    </p>
                </div>
            </li>
            <li>
                <div class="item-inner">
                    <p class="item-title">
                        <a href="https://rulerLwx.gitee.io/2020/07/centos/" class="title">CentOS-</a>
                    </p>
                    <p class="item-date">
                        <time datetime="2020-07-01 18:58:47 &#43;0800 CST" itemprop="datePublished">2020-07-01</time>
                    </p>
                </div>
            </li>
        </ul>
    </div>
</div>
  </div>
</aside>

    
    
<aside class="sidebar sidebar-toc collapse" id="collapseToc" itemscope itemtype="http://schema.org/WPSideBar">
  <div class="slimContent">
    <h4 class="toc-title">Catalogue</h4>
    <nav id="toc" class="js-toc toc">

    </nav>
  </div>
</aside>
<main class="main" role="main"><div class="content">
  <article id="-" class="article article-type-" itemscope
    itemtype="http://schema.org/BlogPosting">
    
    <div class="article-header">
      <h1 itemprop="name">
  <a
    class="article-title"
    href="/2020/07/java-senior-jvm/"
    >Java-Senior-JVM</a
  >
</h1>

      <div class="article-meta">
        
<span class="article-date">
  <i class="icon icon-calendar-check"></i>&nbsp;
<a href="https://rulerLwx.gitee.io/2020/07/java-senior-jvm/" class="article-date">
  <time datetime="2020-07-01 18:58:47 &#43;0800 CST" itemprop="datePublished">2020-07-01</time>
</a>
</span>
<span class="article-category">
  <i class="icon icon-folder"></i>&nbsp;
  <a class="article-category-link" href="/categories/java-senior/"> Java-Senior </a>
</span>  
  <span class="article-tag">
    <i class="icon icon-tags"></i>&nbsp;
    <a class="article-tag-link" href="/tags/jvm/"> jvm </a>
  </span>

        <span class="post-comment"><i class="icon icon-comment"></i>&nbsp;<a href="/2020/07/java-senior-jvm/#comments"
            class="article-comment-link">Comments</a></span>
		<span class="post-wordcount hidden-xs" itemprop="wordCount">Word Count: 3580words</span>
		<span class="post-readcount hidden-xs" itemprop="timeRequired">Read Count: 8minutes </span>
      </div>
    </div>
    <div class="article-entry marked-body js-toc-content" itemprop="articleBody">
      <h1 id="jvm体系结构">JVM体系结构</h1>
<h2 id="jvm的位置">JVM的位置</h2>
<p><img src="https://gitee.com/rulerLwx/PicGo/raw/master/img/20200708021336.png" alt=""></p>
<p>JVM运行在操作系统之上，与硬件没有直接交互</p>
<h2 id="jvm体系结构-1">JVM体系结构</h2>
<p><img src="https://gitee.com/rulerLwx/PicGo/raw/master/img/20200708021356.png" alt=""></p>
<p>灰色代表线程独有，亮色代表线程共享</p>
<p>绿色的就相当于Java中的<strong>java.lang.Runtime</strong>类</p>
<h3 id="类加载器classloader">类加载器ClassLoader</h3>
<p>负责加载class文件，转换成<strong>方法区</strong>中运行时数据结构，并且ClassLoader<strong>只负责class文件的加载</strong>，至于它是否可以运行，则由Execution Engine决定</p>
<p>class文件在文件开头有特定的文件标识<code>cafe babe</code>（用文本编辑器打开class文件就能看到）</p>
<p>类加载有以下4类：</p>
<ul>
<li>Bootstrap 根类加载器，C++语言</li>
<li>Extension 扩展类加载器，Java语言</li>
<li>System 系统类加载器</li>
<li>自定义类加载器，java.lang.ClassLoader的子类</li>
</ul>
<p>Bootstrap 负责加载Java的核心类（jdk1.7.0_79\jre\lib\rt.jar），String、System等类就在这个包中。</p>
<p>Extension负责加载jdk1.7.0_79\jre\lib\ext\*（或java.ext.dirs系统属性指定）的目录下的jar包类。</p>
<p>System负责加载 CLASSPATH环境变量（或-classpath、或java.class.path属性）所指定的jar包和类路径。</p>
<pre><code class="language-java">public static void main(String[] args) {
    Object o = new Object();
    System.out.println(o.getClass().getClassLoader());//null

    MyClass myClass = new MyClass();
    System.out.println(myClass.getClass().getClassLoader().getParent().getParent());//null
    System.out.println(myClass.getClass().getClassLoader().getParent());//sun.misc.Launcher$ExtClassLoader@776ec8df
    System.out.println(myClass.getClass().getClassLoader());//sun.misc.Launcher$AppClassLoader@18b4aac2
}
</code></pre>
<p>类加载遵从<strong>父类委托机制</strong>（又称“双亲委托机制”）</p>
<p>为什么要用父类委托机制？可以起到<strong>沙箱安全</strong>的作用。</p>
<p>如下，自己编写一个java.lang.String类，运行会报错，起到：用户写的类不会污染java提供的类 ——20191108</p>
<pre><code class="language-java">package java.lang;

public class String {
    //错误: 在类 java.lang.String 中找不到 main 方法
    public static void main(String[] args) {
        System.out.println(&quot;abc&quot;);
    }
}
</code></pre>
<p>一张有趣的图：</p>
<p><img src="https://gitee.com/rulerLwx/PicGo/raw/master/img/20200708021419.png" alt=""></p>
<h3 id="本地接口方法native">本地接口（方法）native</h3>
<ul>
<li>Native Interface：本地方法接口</li>
<li>Native Method Stack：用于登记native方法，在Execution Engine执行时加载本地方法库</li>
</ul>
<h3 id="pc寄存器">PC寄存器</h3>
<p>Program Counter Register 程序计算器</p>
<p>每个线程都有一个程序计数器，是一个指针，存储下一条指令的地址。是线程私有的，占用内存较少。</p>
<h3 id="方法区method-area">方法区Method Area</h3>
<p>方法区不是存放方法的地方</p>
<p>存储了<strong>每个类的结构信息</strong>（可以理解为模板），如：常量池、字段（静态、非静态）、方法数据、构造函数、普通方法字节码</p>
<p>方法区在不同虚拟机里实现是不一样的，最典型的是永久代（PermGen space）和元空间（Metaspace）</p>
<ul>
<li>永久代（PermGen space）</li>
<li>元空间（Metaspace）</li>
</ul>
<h3 id="java栈java-stack">Java栈Java Stack</h3>
<p>方法进栈，对象进堆</p>
<ul>
<li>面试：程序 = 算法 + 数据结构</li>
<li>工作：程序 = 框架 + 业务逻辑</li>
</ul>
<p>栈存储什么？</p>
<ul>
<li>本地变量（Local Variables）：输入、输出参数，方法内的变量</li>
<li>栈操作（Operand Stack）：记录入栈、出栈操作</li>
<li>栈帧数据（Frame Data）：类文件、方法等</li>
</ul>
<p><img src="https://gitee.com/rulerLwx/PicGo/raw/master/img/20200708021442.png" alt=""></p>
<p>1）方法递归如果没有边界，就会java.lang.StackOverflowError（SOF），StackOverflowError属于Error，不是Exception。</p>
<p>2）栈、堆、方法区如何交互？</p>
<p><img src="https://gitee.com/rulerLwx/PicGo/raw/master/img/20200708021505.png" alt=""></p>
<p>总结：</p>
<ul>
<li>值传递、引用传递</li>
<li>方法区相当于类的模板 —— 20191108</li>
</ul>
<h3 id="堆heap">堆Heap</h3>
<p>堆内存大小可以调节</p>
<ul>
<li>新生代
<ul>
<li>伊甸区（Eden Space）：GC = YGC = Minor GC</li>
<li>幸存0区（Survivor 0 Space），又称S0 = from</li>
<li>幸存1区（Survivor 1 Space），又称S1 = to</li>
</ul>
</li>
<li>老年代（Tenure Generation Space）：Full GC = FGC = Major GC</li>
<li>元空间（永久区）</li>
</ul>
<p>参考：https://www.bilibili.com/video/av70166821?p=25</p>
<p><img src="https://gitee.com/rulerLwx/PicGo/raw/master/img/20200708021537.png" alt=""></p>
<p>如果出现java.lang.OutOfMemoryError异常，说明Java虚拟机堆内存不够，原因有二：</p>
<ul>
<li>堆内存设置不够，可以通过参数-Xms、-Xmx来调整</li>
<li>代码中创建了大量大对象，且长时间不能被回收（有引用）</li>
</ul>
<p>对象生命周期和GC：from区和to区，它们的位置和名份不是固定的，每次GC后会交换，谁空谁是to，如图：</p>
<p><img src="https://gitee.com/rulerLwx/PicGo/raw/master/img/20200708021559.png" alt=""></p>
<p>物理上的堆内存：</p>
<p><img src="https://gitee.com/rulerLwx/PicGo/raw/master/img/20200708021648.png" alt=""></p>
<p>永久区：</p>
<p>永久区是一个常驻内存区域，用于存放JDK自身所携带的Class，Interface的元数据（运行环境必须的类信息），不会被垃圾回收，关闭JVM才释放内存。</p>
<h1 id="堆体系结构">堆体系结构</h1>
<h1 id="堆参数调优">堆参数调优</h1>
<p>JDK7：</p>
<p><img src="https://gitee.com/rulerLwx/PicGo/raw/master/img/20200708021713.png" alt=""></p>
<p>JDK8：</p>
<p><img src="https://gitee.com/rulerLwx/PicGo/raw/master/img/20200708021726.png" alt=""></p>
<p>java8以后，元空间并不在虚拟机中而是使用本机物理内存，仅受本地内存限制，不再受MaxPermSize控制</p>
<p>参数解释：</p>
<ul>
<li>-Xms：设置初始堆内存大小，即堆最小内存，默认为物理内存的1/64</li>
<li>-Xmx：设置最大堆内存大小，默认为物理内存的1/4</li>
<li>-XX:+PrintGCDetails：输出详细的GC处理日志</li>
<li>-XXSurvivorRatio=3：年轻代中Eden区与Survivor区的大小比值（这里示例，eden占3，survivor占2，总共5份）</li>
<li>-XX:MaxDirectMemorySize：</li>
<li>-XX:MaxTenuringThreshold：设置对象在新生代中存活的次数，默认15岁</li>
</ul>
<p>如：-Xmx100m -Xms100m -XX:+PrintGCDetails</p>
<pre><code class="language-tex">Heap
 PSYoungGen      total 29696K, used 4102K [0x00000000fdf00000, 0x0000000100000000, 0x0000000100000000)
  eden space 25600K, 16% used [0x00000000fdf00000,0x00000000fe301a70,0x00000000ff800000)
  from space 4096K, 0% used [0x00000000ffc00000,0x00000000ffc00000,0x0000000100000000)
  to   space 4096K, 0% used [0x00000000ff800000,0x00000000ff800000,0x00000000ffc00000)
 ParOldGen       total 68608K, used 0K [0x00000000f9c00000, 0x00000000fdf00000, 0x00000000fdf00000)
  object space 68608K, 0% used [0x00000000f9c00000,0x00000000f9c00000,0x00000000fdf00000)
 Metaspace       used 3501K, capacity 4496K, committed 4864K, reserved 1056768K
  class space    used 382K, capacity 388K, committed 512K, reserved 1048576K

</code></pre>
<pre><code class="language-java">    public static void main(String[] args) {
        long maxMemory = Runtime.getRuntime().maxMemory();//Java虚拟机试图使用的最大内存
        long totalMemory = Runtime.getRuntime().totalMemory();//Java虚拟机中的内存问题
        System.out.println(&quot;-Xmx:&quot;+maxMemory+&quot;字节，&quot;+(double)maxMemory/1024/1024+&quot;M&quot;);
        System.out.println(&quot;-Xms:&quot;+totalMemory+&quot;字节，&quot;+(double)totalMemory/1024/1024+&quot;M&quot;);
    }
</code></pre>
<p>问：生产环境，如何配置最大、最小内存？</p>
<p>答：最大、最小配置相同，避免GC应用程序时产生卡顿。</p>
<p>问：写一个应用程序，测试OOM：</p>
<p>答：</p>
<p>1）先设置运行的JVM：-Xmx10m -Xms10m -XX:+PrintGCDetails</p>
<p>2）代码：</p>
<pre><code class="language-java">    public static void main(String[] args) {
        byte[] bytes = new byte[40 * 1024 * 1024];
    }
</code></pre>
<h1 id="minorgcmajorgc执行时机">MinorGC/MajorGC执行时机</h1>
<p>Minor GC触发条件：当Eden区满时，触发Minor GC。</p>
<p>Full GC触发条件：</p>
<p>（1）调用System.gc时，系统建议执行Full GC，但是不必然执行</p>
<p>（2）老年代空间不足</p>
<p>（3）方法去空间不足</p>
<p>（4）通过Minor GC后进入老年代的平均大小大于老年代的可用内存</p>
<p>（5）由Eden区、From Space区向To Space区复制时，对象大小大于To Space可用内存，则把该对象转存到老年代，且老年代的可用内存小于该对象大小</p>
<p>参考：</p>
<ul>
<li><a href="https://blog.csdn.net/yhyr_ycy/article/details/52566105">https://blog.csdn.net/yhyr_ycy/article/details/52566105</a></li>
<li><a href="https://blog.csdn.net/summerZBH123/article/details/81131945">https://blog.csdn.net/summerZBH123/article/details/81131945</a></li>
</ul>
<h1 id="gc收集日志信息">GC收集日志信息</h1>
<pre><code class="language-tex">[GC (Allocation Failure) [PSYoungGen: 2040K-&gt;488K(2560K)] 2040K-&gt;904K(9728K), 0.0051501 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
[GC (Allocation Failure) [PSYoungGen: 1318K-&gt;504K(2560K)] 1734K-&gt;1008K(9728K), 0.0020450 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[GC (Allocation Failure) [PSYoungGen: 504K-&gt;504K(2560K)] 1008K-&gt;1048K(9728K), 0.0006964 secs] [Times: user=0.05 sys=0.00, real=0.00 secs] 
[Full GC (Allocation Failure) [PSYoungGen: 504K-&gt;0K(2560K)] [ParOldGen: 544K-&gt;846K(7168K)] 1048K-&gt;846K(9728K), [Metaspace: 3473K-&gt;3473K(1056768K)], 0.0088245 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
[GC (Allocation Failure) [PSYoungGen: 0K-&gt;0K(2560K)] 846K-&gt;846K(9728K), 0.0003925 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[Full GC (Allocation Failure) [PSYoungGen: 0K-&gt;0K(2560K)] [ParOldGen: 846K-&gt;828K(7168K)] 846K-&gt;828K(9728K), [Metaspace: 3473K-&gt;3473K(1056768K)], 0.0093239 secs] [Times: user=0.02 sys=0.00, real=0.01 secs] 
Exception in thread &quot;main&quot; java.lang.OutOfMemoryError: Java heap space
</code></pre>
<p>GC：</p>
<p><img src="https://gitee.com/rulerLwx/PicGo/raw/master/img/20200708021751.png" alt=""></p>
<p>FullGC：</p>
<p><img src="https://gitee.com/rulerLwx/PicGo/raw/master/img/20200708021803.png" alt=""></p>
<h1 id="gc四大算法">GC四大算法</h1>
<p><a href="https://www.bilibili.com/video/av70166821?p=31">https://www.bilibili.com/video/av70166821?p=31</a></p>
<h2 id="gc是什么">GC是什么</h2>
<p>分代收集算法</p>
<ul>
<li>次数上频繁收集Yong区</li>
<li>次数上较少收集Old区</li>
<li>基本上不动Perm区（又叫元空间）</li>
</ul>
<h2 id="算法概述">算法概述：</h2>
<p>Major GC与Minor GC区别：</p>
<ul>
<li>Minor GC发生在新生代，Major GC发生在老年代</li>
<li>Major GC比Minor GC慢10倍以上</li>
</ul>
<h2 id="4大算法">4大算法</h2>
<ul>
<li>引用计数法</li>
<li>复制算法（Copying）</li>
<li>标记清除（Mark-Sweep）</li>
<li>标记压缩（Mark-Compact）</li>
</ul>
<h3 id="引用计数法">引用计数法</h3>
<p><img src="https://gitee.com/rulerLwx/PicGo/raw/master/img/20200708021950.png" alt=""></p>
<p>引用说的是：此对象上还有多少个引用正在使用</p>
<p>手动调用<code>System.gc();</code>并不会立即执行垃圾回收</p>
<h3 id="复制算法copying">复制算法（Copying）</h3>
<p>复制算法（Copying）发生在<strong>新生代</strong></p>
<p>新生代使用的是Minor GC，这种GC使用就是复制算法</p>
<p>From区、to区复制来复制去，使用的就是这种算法</p>
<p>复制算法的<strong>基本思想</strong>是将内存分为2块，每次只用一块，<del>当一块用完</del>，就将活着的对象复制到另一块上</p>
<p><strong>优缺点</strong>：复制算法不会产生内存碎片，但会浪费内存（没有使用的那块）</p>
<p><img src="https://gitee.com/rulerLwx/PicGo/raw/master/img/20200708022013.png" alt=""></p>
<p>如何复制：</p>
<p><img src="https://gitee.com/rulerLwx/PicGo/raw/master/img/20200708022025.png" alt=""></p>
<h3 id="标记清除mark-sweep">标记清除（Mark-Sweep）</h3>
<p>标记清除（Mark-Sweep）发生在<strong>老年代</strong></p>
<p>原理：分为标记和清除两部分，先标记出要回收的对象，然后统一回收这些对象</p>
<p><img src="https://gitee.com/rulerLwx/PicGo/raw/master/img/20200708022044.png" alt=""></p>
<p>优缺点：不需要额外空间，但两次扫描，耗时严重，会产生内存碎片。</p>
<h3 id="标记压缩mark-compact">标记压缩（Mark-Compact）</h3>
<p>标记压缩（Mark-Compact）发生在<strong>老年代</strong></p>
<p>也可以叫标记-清除-压缩（Mark-Sweep-Compact）</p>
<p><img src="https://gitee.com/rulerLwx/PicGo/raw/master/img/20200708022102.png" alt=""></p>
<p>优缺点：没有内存碎片，但耗时长</p>
<h3 id="总结">总结</h3>
<p>有没有最好的垃圾回收算法？</p>
<p>答：没有。应该分代使用相应的算法，简称“分代算法”。</p>
<p>新生代的特点是区域相对老年代小、对象存活率低</p>
<p>老年代的特点是区域大、对象存活率高</p>
<p><img src="https://gitee.com/rulerLwx/PicGo/raw/master/img/20200708022133.png" alt=""></p>
<h1 id="jmm">JMM</h1>
<p>JMM，Java Memory Model，Java内存模型</p>
<h2 id="什么是java内存模型">什么是Java内存模型</h2>
<p>物理内存 &ndash;&gt; 工作内存 &ndash;&gt; 缓存 &ndash;&gt; CPU</p>
<h2 id="heading"></h2>
<p>Java内存模型是高并发开发的基础</p>
<p>高并发编程需要满足以下3点：</p>
<ul>
<li>可见性</li>
<li>原子性</li>
<li>有序性</li>
</ul>

    </div>
    <div class="article-footer">
<blockquote class="mt-2x">
  <ul class="post-copyright list-unstyled">
    <li class="post-copyright-link hidden-xs">
      <strong>Permalink: </strong>
      <a href="https://rulerLwx.gitee.io/2020/07/java-senior-jvm/" title="Java-Senior-JVM" target="_blank" rel="external">https://rulerLwx.gitee.io/2020/07/java-senior-jvm/</a>
    </li>
    <li class="post-copyright-license">
      <strong>License：</strong><a href="http://creativecommons.org/licenses/by/4.0/deed.zh" target="_blank" rel="external">CC BY 4.0 CN</a>
    </li>
  </ul>
</blockquote>

<div class="panel panel-default panel-badger">
  <div class="panel-body">
    <figure class="media">
      <div class="media-left">
        <a href="https://gitee.com/rulerLwx" target="_blank" class="img-burn thumb-sm visible-lg">
          <img src="https://rulerLwx.gitee.io/avatar.png" class="img-rounded w-full" alt="">
        </a>
      </div>
      <div class="media-body">
        <h3 class="media-heading"><a href="https://gitee.com/rulerLwx" target="_blank"><span class="text-dark">rulerLwx</span><small class="ml-1x">thinking...</small></a></h3>
        <div>Good Good Study, Day Day Up~</div>
      </div>
    </figure>
  </div>
</div>
    </div>
  </article>
<section id="comments">
    <div id="vcomments"></div>
</section>

</div><nav class="bar bar-footer clearfix" data-stick-bottom>
    <div class="bar-inner">
        <ul class="pager pull-left">
            <li class="prev">
                <a href="https://rulerLwx.gitee.io/2020/07/java-senior-uml/" title="Java-Senior-UML"><i
                        class="icon icon-angle-left"
                        aria-hidden="true"></i><span>&nbsp;&nbsp;Older</span></a>
            </li>
            <li class="next">
                <a href="https://rulerLwx.gitee.io/2020/07/java-se-%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1/"
                    title="Java-Se-面向对象"><span>Newer&nbsp;&nbsp;</span><i
                        class="icon icon-angle-right" aria-hidden="true"></i></a>
            </li>
            
            <li class="toggle-toc">
                <a class="toggle-btn collapsed" data-toggle="collapse" href="#collapseToc" aria-expanded="false"
                    title="Catalogue" role="button">
                    <span>[&nbsp;</span><span>Catalogue</span>
                    <i class="text-collapsed icon icon-anchor"></i>
                    <i class="text-in icon icon-close"></i>
                    <span>]</span>
                </a>
            </li>
        </ul>
        <div class="bar-right">
            <div class="share-component" data-sites="weibo,qq,wechat"
                data-mobile-sites="weibo,qq,qzone"></div>
        </div>
    </div>
</nav>

</main><footer class="footer" itemscope itemtype="http://schema.org/WPFooter">
<ul class="social-links">
    <li><a href="https://gitee.com/rulerLwx" target="_blank" title="gitee" data-toggle=tooltip data-placement=top >
            <i class="icon icon-gitee"></i></a></li>
    <li><a href="https://github.com/wolf-lea" target="_blank" title="github" data-toggle=tooltip data-placement=top >
            <i class="icon icon-github"></i></a></li>
</ul>
  <div class="copyright">
    &copy;2020  -
    2020
    <div class="publishby">
        Theme by <a href="https://github.com/xiaoheiAh" target="_blank"> xiaoheiAh </a>base on<a href="https://github.com/xiaoheiAh/hugo-theme-pure" target="_blank"> pure</a>.
    </div>
  </div>
</footer>

<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.2/MathJax.js?config=TeX-MML-AM_SVG"></script>
<script type="text/x-mathjax-config">
    MathJax.Hub.Config({
            showMathMenu: false, //disables context menu
            tex2jax: {
            inlineMath: [ ['$','$'], ['\\(','\\)'] ]
           }
    });
</script>


<script src="https://cdn.jsdelivr.net/npm/jquery@3.4.1/dist/jquery.min.js"></script>
<script>
    window.jQuery || document.write('<script src="js/jquery.min.js"><\/script>')
</script>
<script type="text/javascript" src="https://cdn.staticfile.org/highlight.js/9.15.10/highlight.min.js"></script>
<script type="text/javascript" src="https://cdn.staticfile.org/highlight.js/9.15.10/languages/python.min.js" defer></script>
<script type="text/javascript" src="https://cdn.staticfile.org/highlight.js/9.15.10/languages/javascript.min.js" defer></script><script>
    hljs.configure({
        tabReplace: '    ', 
        classPrefix: ''     
        
    })
    hljs.initHighlightingOnLoad();
</script>
<script src="https://rulerLwx.gitee.io/js/application.min.bdeb64b910570b6c41badc6a05b7afb0c8ad9efd8525de3c7257d59e786326a3.js"></script>
<script src="https://rulerLwx.gitee.io/js/plugin.min.51ff8c7317566f82259170fa36e09c4493adc9b9378b427a01ad3f017ebac7dd.js"></script>

<script>
    (function (window) {
        var INSIGHT_CONFIG = {
            TRANSLATION: {
                POSTS: 'Posts',
                PAGES: 'Pages',
                CATEGORIES: 'Categories',
                TAGS: 'Tags',
                UNTITLED: '(Untitled)',
            },
            ROOT_URL: 'https:\/\/rulerLwx.gitee.io',
            CONTENT_URL: 'https:\/\/rulerLwx.gitee.io\/searchindex.json ',
        };
        window.INSIGHT_CONFIG = INSIGHT_CONFIG;
    })(window);
</script>
<script type="text/javascript" src="https://rulerLwx.gitee.io/js/insight.min.a343cd9a5a7698336b28ef3a7c16a3a1b1d2d5fb17dc8ed04022bbe08cc5459073a15bdafa3a8a58cdd56080784bdd69fa70b1ae8597565c799c57ed00f0e120.js" defer></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tocbot/4.4.2/tocbot.min.js"></script>
<script>
    tocbot.init({
        
        tocSelector: '.js-toc',
        
        contentSelector: '.js-toc-content',
        
        headingSelector: 'h1, h2, h3',
        
        hasInnerContainers: true,
    });
</script>

<script src="https://cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/valine"></script>
<script type="text/javascript">
    var GUEST = ['nick', 'mail', 'link'];
    var meta = 'nick,mail';
    meta = meta.split(',').filter(function (item) {
        return GUEST.indexOf(item) > -1;
    });
    new Valine({
        el: '#vcomments',
        verify: null ,
        notify: null ,
        appId: 'IyAB0PSPRazTPDxitO1ddQ7O-gzGzoHsz',
        appKey: '5rBJTq4KidYF33eXwvRVhtEH',
        placeholder: 'enjoy~',
        avatar: 'mm',
        meta: meta,
        pageSize: '10' || 10,
        visitor: false 
});
</script>

  </body>
</html>
